package com.mac.bluetoothbt.feature;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import static com.mac.bluetoothbt.feature.Main2Activity.DEVICESTATE;


/**
 * @author wyh
 */
public class SettingActivity extends AppCompatActivity {

    private final String pwd4 = "6676";
    private String password = "";
    private BroadcastReceiver broadcastReceiver = null;
    DeviceEntity deviceEntity;
    private boolean is6passwordCorrect = false;
    private boolean is8passwordCorrect = false;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_password);
        broadcastReceiver = new SettingAcrtivityBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Actions.ACTION_PASSWORDGOT);
        intentFilter.addAction(Actions.ACTION_REMAINTIME);
        registerReceiver(broadcastReceiver, intentFilter);
        Intent intent = getIntent();
        if (intent.hasExtra(DEVICESTATE)){
            deviceEntity = (DeviceEntity) intent.getSerializableExtra(DEVICESTATE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        EditText passwordEditText = findViewById(R.id.editText);
        passwordEditText.setText("");
        sendBroadcast(new Intent(Actions.ACTION_TIMER_TRIG));
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }


    /**
     * 输入password界面确认onClick
     * @param view View对象
     */
    public void onPasswordConfirmClicked(View view){
        EditText passwordEditText = findViewById(R.id.editText);
        password = passwordEditText.getText().toString();
        //默认4位密码
        if (pwd4.equals(password)){
            DeviceEntity glbDeviceEntity = ((App)getApplication()).getDeviceEntity();
            DeviceEntity anotherDeviceEntity = new DeviceEntity(deviceEntity);
            if (glbDeviceEntity != anotherDeviceEntity) {
                anotherDeviceEntity = new DeviceEntity(glbDeviceEntity);
            }
            //跳转设置页面
            startActivity(new Intent()
                    .setClass(getApplicationContext(), Setting1Activity.class)
                    .putExtra(DEVICESTATE, anotherDeviceEntity));
            return;
        }

        if (password.length() == 4) {FrameUtil.readPassword4(getApplicationContext());}
        else if(password.length() == 6) {FrameUtil.readPassword6(getApplicationContext());}
        else if(password.length() == 8) {FrameUtil.readPassword8(getApplicationContext());}
    }

    /**
     * 输入password界面取消按下
     * @param view View
     */
    public void onPasswordCancelClicked(View view){
        finish();
    }

    private class SettingAcrtivityBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent){
            if (intent.getAction() == null) {return;}
            switch (intent.getAction()){
                case Actions.ACTION_PASSWORDGOT:
                    String passwordRecv = intent.getStringExtra("password");
                    Log.d("DEBUG", "获得Password" + passwordRecv);
                    String passwordInput = ((EditText)findViewById(R.id.editText)).getText().toString();
                    int length = passwordInput.length();
                    String pwd = passwordRecv.substring(0, length);
                    boolean pass = passwordInput.equals(pwd);
                    if (pass){
                        Toast.makeText(getApplicationContext(), "密码验证成功", Toast.LENGTH_SHORT).show();
                        switch (length){
                            case 4:
                                DeviceEntity anotherDeviceEntity = new DeviceEntity(deviceEntity);
                                startActivity(new Intent().setClass(getApplicationContext(), Setting1Activity.class).putExtra(DEVICESTATE, anotherDeviceEntity));//跳转设置页面
                                break;
                            case 6:
                                is6passwordCorrect = true;
                                FrameUtil.readremaintime6(getApplicationContext());
                                break;
                            case 8:
                                is8passwordCorrect = true;
                                FrameUtil.readremaintime8(getApplicationContext());
                                break;
                            default:
                                break;
                        }

                    }else {
                        Toast.makeText(getApplicationContext(), "密码验证失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case Actions.ACTION_REMAINTIME:
                    if (!(is6passwordCorrect || is8passwordCorrect)) {
                        break;
                    } else {
                        is8passwordCorrect = false;
                        is6passwordCorrect = false;
                    }
                    int remian = intent.getIntExtra("data", 0);
                    startActivity(new Intent().setClass(getApplicationContext(), RemainTime.class).putExtra("remaintime", remian).putExtra("password", password));
                    break;
                default:
                    break;
            }
        }
    }
}
